﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PlcConnectionInfoController : ControllerBase
    {
        private readonly ILogUtil _LogUtil;
        private readonly PlcConnectionInfoApp _PlcConnectionInfoApp;
        public PlcConnectionInfoController(ILogUtil logUtil, PlcConnectionInfoApp plcConnectionInfoApp)
        {
            _LogUtil = logUtil;
            _PlcConnectionInfoApp = plcConnectionInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<List<PlcConnectionInfo>>> GetPagedPlcConnectionInfoAsync(QueryPlcConnectionInfoDto queryPlcConnectionInfo)
        {
            HeaderResult<List<PlcConnectionInfo>> headerResult = new HeaderResult<List<PlcConnectionInfo>>();
            try
            {
                PageDto<PlcConnectionInfo> pageDto = await _PlcConnectionInfoApp.GetPagedPlcConnectionInfoAsync(queryPlcConnectionInfo);
                headerResult.IsSucceed = true;
                headerResult.Result = pageDto.Result;
                headerResult.Total = pageDto.Total;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<List<PlcConnectionInfo>>> GetPlcConnectionInfoAsync()
        {
            HeaderResult<List<PlcConnectionInfo>> headerResult = new HeaderResult<List<PlcConnectionInfo>>();
            try
            {
               var res= await _PlcConnectionInfoApp.GetPlcConnectionInfoAsync();
                headerResult.IsSucceed = true;
                headerResult.Result = res;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> CreatePlcConnectionInfoAsync(PlcConnectionInfo plcConnectionInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _PlcConnectionInfoApp.CreatePlcConnectionInfoAsync(plcConnectionInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "新增成功" : "新增失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ModifyPlcConnectionInfoAsync(PlcConnectionInfo plcConnectionInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _PlcConnectionInfoApp.ModifyPlcConnectionInfoAsync(plcConnectionInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> DeletePlcConnectionInfoAsync(List<PlcConnectionInfo> plcConnectionInfoList)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _PlcConnectionInfoApp.DeletePlcConnectionInfoAsync(plcConnectionInfoList);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "删除成功" : "删除失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        #region plc连接锁定与状态相关
        [HttpPost]
        public async Task<HeaderResult<List<PlcConnectionLockInfoDto>>> GetPlcConnectionLockListAsync(QueryPlcConnectionLockInfoDto queryPlcConnectionLockInfoDto)
        {
            HeaderResult<List<PlcConnectionLockInfoDto>> headerResult = new HeaderResult<List<PlcConnectionLockInfoDto>>();
            try
            {
                List<PlcConnectionLockInfoDto> result = await _PlcConnectionInfoApp.GetPlcConnectionLockInfoAsync(queryPlcConnectionLockInfoDto);
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<List<PlcConnectionStateInfoDto>>> GetPlcConnectionStateListAsync(QueryPlcConnectionStateInfoDto queryPlcConnectionStateInfoDto)
        {
            HeaderResult<List<PlcConnectionStateInfoDto>> headerResult = new HeaderResult<List<PlcConnectionStateInfoDto>>();
            try
            {
                List<PlcConnectionStateInfoDto> result = await _PlcConnectionInfoApp.GetPlcConnectionStateInfoAsync(queryPlcConnectionStateInfoDto);
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }
        #endregion
    }
}
